//创建用户相关的小仓库
import { defineStore } from "pinia";
//引入请求函数
import { reqCount, reqdevices, reqdevice, reqtemplate, reqLastProdevent, reqTerminal, reqExport } from "@/api/device/device";
// 引入组件库
import { ElMessage } from "element-plus";

/**
 * 用户信息
 * @methods  获取设备信息
 */
export const useDevicesStore = defineStore("devicesStore", {
  state: () => ({
    deviceCount: {},
    devices: <any>[],   //所有设备的卡片式综合数据
    deviceInfo: <any>[],   //设备的卡片式综合数据
    lastProdeventinfo: <any>[],   //最后一条生产事件信息
    lastProdeventinfoself: <any>[],   //单个设备最后一条生产事件信息
    templateinfo: <any>[],
    hardwarerateinfo: {},  //硬件状态的信息

    allTerminalInfo: [],//所有终端信息
    exportInfo: {}
  }),
  //书写小仓库方法的地方
  actions: {
    async count() {
      try {
        let res: any = await reqCount();
        this.deviceCount = res
      } catch (error) {
        console.log(error);
      }
    },


    //获取所有设备的卡片式综合数据
    async Alldevice() {
      try {
        let res: any = await reqdevices();
        // 进行仓库存储
        this.devices = res;
      } catch (error) {
        console.log(error);
      }
    },

    //获取设备的卡片式综合数据
    async deviceinfo(terminalIdentity: any) {
      try {
        let res = await reqdevice(terminalIdentity)
        this.deviceInfo.push(res)
        this.hardwarerateinfo = res.Hardware
      } catch (error) {
        console.log(error);
      }
    },

    // 获取指定设备的隶属模板信息
    async template(TerminalIdentity: any) {
      try {
        let res: any = await reqtemplate(TerminalIdentity);
        this.templateinfo = res
      } catch (error) {
        console.log(error);
      }
    },

    // 获取根据终端标识符查询最后一条生产事件信息
    async lastProdevent(terminalIdentity: any) {
      try {
        let res = await reqLastProdevent(terminalIdentity)
        this.lastProdeventinfo.push(res)
        this.lastProdeventinfoself = res

      } catch (error) {
        console.log(error);
      }
    },

    //获取所有终端信息
    async allTerminal() {
      try {
        let res = await reqTerminal()
        this.allTerminalInfo = res
      } catch (error) {
        console.log(error);
      }

    },

    //导出终端信息配置文件
    async exportTerminal(terminalId: any) {
      try {
        let res = await reqExport(terminalId)
        this.exportInfo = res
      } catch (error) {
        console.log(error);
      }

    }
  },
});
